<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="/template.xhtml">
        <ui:define name="title">
            <h:outputText value="Pedidos"></h:outputText>
        </ui:define>
        <ui:define name="body">
            <h:form  id="formPedidos">
                <div align="left" style="margin-bottom: 15px;">
                    <h:commandButton styleClass="btn btn-mini btn-primary" action="#{pedidoController.imprimirEntrega()}" value="Imprimir guias"/>
                    <p:spacer width="10" height="10"/>
                    <h:commandButton styleClass="btn btn-mini btn-primary" action="/cadastro/cliente/List.xhtml" value="Novo pedido"/>
                </div>
                <h:panelGroup id="grupPedidos">
                    <fieldset>
                        <div align="left">
                            <legend>
                                <h:outputText value="#{pedidoController.pagination.pageFirstItem + 1}..#{pedidoController.pagination.pageLastItem + 1}/#{pedidoController.pagination.itemsCount}"/>&nbsp;
                                <h:commandLink action="#{pedidoController.previous}" value="#{bundle.Previous} #{pedidoController.pagination.pageSize}" disabled="#{!pedidoController.pagination.hasPreviousPage}"/>&nbsp;
                                <h:commandLink action="#{pedidoController.next}" value="#{bundle.Next} #{pedidoController.pagination.pageSize}" disabled="#{!pedidoController.pagination.hasNextPage}"/>&nbsp;
                            </legend>
                        </div>
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <td  align="left" colspan="2">
                                        <h:outputText value="Status:"/>
                                        <h:selectOneMenu value="#{pedidoController.statusFiltro}" id="statusFiltro">
                                            <f:selectItem itemLabel="Todos" itemValue="#{null}"/>
                                            <f:selectItems value="#{pedidoController.statusPedidoSelect}"/>
                                            <p:ajax listener="#{pedidoController.recreateModel()}" update=":formPedidos"/>
                                        </h:selectOneMenu>
                                    </td>
                                    <td colspan="1" align="left">
                                        <h:outputText value="Telefone:"/>
                                        <h:panelGroup>
                                            <p:autoComplete value="#{pedidoController.telFiltro}"
                                                            completeMethod="#{clienteController.complete}"
                                                            forceSelection="false" 
                                                            id="telefone" size="10" maxlength="8">
                                                <p:ajax event="itemSelect" listener="#{pedidoController.recreateModel()}" update=":formPedidos"/>
                                            </p:autoComplete>
                                            <p:commandButton styleClass="btn btn-mini btn-primary" action="#{pedidoController.recreateModel()}" update=":formPedidos" icon="ui-icon-search"/>
                                        </h:panelGroup>
                                    </td>
                                    <td  align="left" colspan="3">
                                        <h:outputText value="Nome:"/>
                                        <p:inputText value="#{pedidoController.nomeFiltro}"
                                                     id="nomeFiltro" size="20" maxlength="8">
                                            <p:ajax event="blur" listener="#{pedidoController.recreateModel()}" update=":formPedidos"/>
                                        </p:inputText>
                                    </td>
                                </tr>
                                <tr>
                                    <th align="left"><h:outputText value="Código" style="font-style: oblique;"/></th>
                                    <th align="left"><h:outputText value="Data" style="font-style: oblique;"/></th>
                                    <th align="left"><h:outputText value="Cliente" style="font-style: oblique;"/></th>
                                    <th align="left"><h:outputText value="Status" style="font-style: oblique;"/></th>
                                    <th align="left"></th>
                                    <th align="left"><h:outputText value="Imprimir" style="font-style: oblique;"/></th>
                                </tr>
                            </thead>
                            <tbody>
                                <ui:repeat value="#{pedidoController.items}" var="item">
                                    <tr>
                                        <td align="left" width="60"><h:outputText value="#{item.id}"/></td>
                                        <td align="left" width="80">
                                            <h:outputText value="#{item.dataPed}">
                                                <f:convertDateTime pattern="dd/MM/yyyy" />
                                            </h:outputText>
                                        </td>
                                        <td align="left"><h:outputText value="#{item.cliente.label}"/></td>
                                        <td align="left"><h:outputText value="#{item.status.status}"/></td>
                                        <td align="left" width="230">
                                            <h:commandLink action="#{pedidoController.prepareView}" value="Ver pedido"/>
                                            <p:spacer height="3" width="10"/>
                                            <h:commandLink action="#{pedidoController.destroy}" value="Cancelar" onclick="return confirme('Confirma o cancelamento?')" rendered="#{item.status eq 'PENDENTE'}" style="color: red;"/>
                                            <p:spacer height="3" width="10"/>
                                            <h:commandLink action="#{pedidoController.finalizarPedido(item)}" value="Finalizar" onclick="return confirme('Confirma a finalização?')" rendered="#{item.status eq 'PENDENTE'}" style="color: orange;"/>
                                        </td>
                                        <td align="left">
                                            <h:selectBooleanCheckbox value="#{item.marcado}" rendered="#{item.status eq 'PENDENTE'}" style="margin-left: 20px;"/>
                                        </td>   
                                    </tr>
                                </ui:repeat>
                            </tbody>
                            <h:panelGroup rendered="#{pedidoController.items.rowCount == 0}">
                                <tfoot>
                                    <tr>
                                        <td align="left" colspan="6"><h:outputText escape="false" value="Sem dados" /></td>
                                    </tr>
                                </tfoot>
                            </h:panelGroup>
                        </table>
                    </fieldset>
                </h:panelGroup>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
